import { IFeidaoAiBrowserComponent } from "@feidao/web/interfaces";
import nodejs from "@feidao/web/atom/browser/msg/nodejs";
import { post } from '@feidao/web/request';

export default async function upload(fd: IFeidaoAiBrowserComponent, id: string, node: HTMLInputElement) {
	const spaceid = fd.data.spaceid;
	const host = 'http://192.168.40.195';

	const file = node.files[0];
	const file_name = file.name;
	const r = new FileReader();
	r.readAsDataURL(file);
	return new Promise((resolve, reject) => {
		r.onload = async function (e) {
			const data = (e.target as any).result as string;
			const res = await nodejs(spaceid, 'hr_upload', { host, spaceid, id, data, file_name });
			resolve(res);
		}
		r.onerror = async function (e) {
			reject('fail');
		}
	});
}